# -*- coding: utf-8 -*-
# Houdini To Arnold
# Copyright (C) 2013 Erkan Ozgur Yilmaz
#
# This file is part of Houdini To Arnold.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation;
# version 2.1 of the License.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA

import hou
import math
from ArnoldBase import Base


class Camera(Base):
    """Arnold default camera
    """

    __prefix__ = 'camera'

    def __init__(self, **kwargs):
        super(Camera, self).__init__(**kwargs)
        self.params.update(kwargs)

    def get_name(self, path):
        """returns the camera name from the given path
        """
        camera = hou.node(path)
        return camera.name().replace('/', '_')

    def generate_params(path):
        """generates camera parameters from the given path
        """
        camera = hou.node(path)

        apx = camera.evalParm('aperture')
        focal = camera.evalParm('focal')
        fovx = math.degrees(2 * math.atan((apx * 0.5) / focal))

        return {
            'camera_name': camera.name().replace('/', '_'),
            'fov': fovx,
            'focus_distance': camera.evalParm('focus'),
            'uv_remap': [0, 0, 0, 0],
            'matrix': camera.worldTransform().asTuple(),
            'near_clip': camera.evalParm('near'),
            'far_clip': camera.evalParm('far'),
            'screen_window_min': [-1, -1],
            'screen_window_max': [1, 1],
            'exposure': 0
        }

    def to_ass(self):
        """exports camera data with the given params
        """
        # condition some parameters
        self.params['uv_remap'] = '%s %s %s %s' % tuple(self.params['uv_remap'])
        self.params['matrix'] = ' %s %s %s %s' + \
                                ' %s %s %s %s' + \
                                ' %s %s %s %s' + \
                                ' %s %s %s %s' % self.params['matrix']
        self.params['screen_window_min'] = '%s %s' % tuple(
            self.params['screen_window_min']
        )
        self.params['screen_window_max'] = '%s %s' % tuple(
            self.params['screen_window_max']
        )

        template = """persp_camera
{
 name %(camera_name)s
 fov %(fov)f
 focus_distance %(focus_distance)f
 aperture_size %(aperture_size)f
 aperture_blades %(aperture_blades)i
 aperture_rotation %(aperture_rotation)f
 aperture_blade_curvature %(aperture_blade_curvature)f
 uv_remap %(uv_remap)s
 matrix
 %(matrix)s
 near_clip %(near_clip)f
 far_clip %(far_clip)f
 screen_window_min %(screen_window_min)s
 screen_window_max %(screen_window_max)s
 exposure %(exposure)f
}

    """
        return template % self.params

    def generate_data_from_path(self, path):
        return self.generate_data(self.generate_params(path))


class PerspCamera(Camera):
    """A perspective camera
    """
    __prefix__ = 'persp_camera'


class OrthoCamera(Camera):
    """An orthographic camera
    """
    __prefix__ = 'ortho_camera'


